package com.rykj

import android.app.Application
import android.util.Log
import com.lcw.library.imagepicker.ImagePicker
import com.lcw.library.imagepicker.utils.Util
import com.rykj.api.OnErrorCodeIntercept
import com.rykj.util.CacheHelper
import com.rykj.util.CommonUtil
import com.rykj.util.LogX
import com.rykj.util.PrefUtility
import com.rykj.widget.GlideLoader
import com.tencent.bugly.Bugly
import com.tencent.bugly.crashreport.CrashReport.UserStrategy
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.QbSdk.PreInitCallback
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.BuildConfig

/**
 * Description: author: zhangsan on 2017/6/20 下午5:54.
 */
object AppProxy {

    @JvmStatic private val TAG = "App"

    @JvmField var application: Application? = null

    fun onCreate(application: Application) {
        this.application = application
        AutoSize.initCompatMultiProcess(application)
        ImagePicker.getInstance().setImageLoader(GlideLoader()) //设置自定义图片加载器
        CommonUtil.setAppContext(application)
        PrefUtility.initPre(application)
        Util.setUriAuthority(application)
        LogX.init(BuildConfig.DEBUG, "haoche")

        val mainProcess = CommonUtil.isMainProcess(application)
        val strategy = UserStrategy(application)
        strategy.isBuglyLogUpload = mainProcess
        // 初始化Bugly
        Bugly.init(application, "4c1f7c365c", false, strategy)

        if (mainProcess) {
            CacheHelper.getInstance().requestData()
        }

        initTBS()

        //        initJPush();
    }

    //    private void initJPush() {
//        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
//        JPushInterface.init(this);            // 初始化 JPush
//    }

    /**
     * 初始化TBS浏览服务X5内核
     */
    private fun initTBS() { //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
//        QbSdk.setDownloadWithoutWifi(true);//非wifi条件下允许下载X5内核
        val cb: PreInitCallback = object : PreInitCallback {
            override fun onCoreInitFinished() {}
            override fun onViewInitFinished(arg0: Boolean) { //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d(TAG, " 初始化TBS浏览服务X5内核  is $arg0")
            }
        }
        //  x5内核初始化接口
        QbSdk.initX5Environment(application, cb)
    }

    /**
     * 错误码拦截器
     * */
    public var onErrorCodeIntercept: OnErrorCodeIntercept? = null

    object EmptyConfig {

        @JvmField var ERROR_IMAGE = 0

        @JvmField var EMPTY_IMAGE = 0
    }
}